﻿using UnityEngine;
using UnityEngine.UIElements;
using winS.UnityEditor.UIElement;
using static winS.UnityEditor.ProcessEditor.ProcessEditorData;

namespace winS.UnityEditor.ProcessEditor
{
    /// <summary>
    /// 连接器
    /// </summary>
    public class Connector : VisualElement
    {
        public static readonly Color enableColor = new Color32(132, 228, 231, 255);
        public static readonly Color disableColor = new Color32(55, 55, 55, 255);

        public bool isHighlighting { get; private set; }

        public Color highlightColor { get => _highlightColor; set => SetHighlightColor(value); }

        public readonly VisualElement box;
        public readonly VisualElement cap;

        private Color _highlightColor = ConnectionLineSetting.defaultHighlightColor;

        public Connector()
        {
            name = "端口连接器";
            style.SetPadding(4f);//扩大选取范围, 防止太小选不到

            Add(box = EditorElementUtility.elementFactory.CreateEditorElementWithName("连接器外壳"));
            box.style.SetBorder(enableColor, 1f, 8f);
            box.style.alignItems = Align.Center;
            box.style.justifyContent = Justify.Center;
            box.style.height = box.style.width = 8f;
            box.style.SetBackgroundColor(32, 32, 32);

            box.Add(cap = EditorElementUtility.elementFactory.CreateEditorElementWithName("连接器盖子"));
            cap.style.SetBorderRadius(4f);
            cap.style.SetBackgroundColor(enableColor);
            cap.style.height = cap.style.width = 4f;
            HideCap();
        }

        public void ShowCap()
        {
            cap.visible = true;
        }
        public void HideCap()
        {
            cap.visible = false;
        }
        public void HideBox()
        {
            box.style.SetBorderColor(disableColor);
        }
        public void ShowBox()
        {
            box.style.SetBorderColor(enableColor);
        }
        public void EnterNormaltState()
        {
            if (!isHighlighting) return;
            isHighlighting = false;
            box.style.SetBorderColor(enableColor);
            cap.style.SetBackgroundColor(enableColor);
        }
        public void EnterHighlightState()
        {
            if (isHighlighting) return;
            isHighlighting = true;
            box.style.SetBorderColor(highlightColor);
            cap.style.SetBackgroundColor(highlightColor);
        }

        private void SetHighlightColor(Color newHighlightColor)
        {
            _highlightColor = newHighlightColor;
            if (!isHighlighting) return;
            box.style.SetBorderColor(highlightColor);
            cap.style.SetBackgroundColor(highlightColor);
        }
    }
}